package com.ruoyi;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.KongAdmin;

import java.net.MalformedURLException;
import java.net.URL;

public class Testkong {

    public static void main(String[] args) {
        KongAdmin kongAdmin = new KongAdmin();
      //  kong创建一个服务
//        JSONObject requestBody = new JSONObject();
//        requestBody.put("name", "kongtest6");
//        //host要具体ip
//        requestBody.put("host", "172.21.176.1");
//        requestBody.put("port", 8082);
//        requestBody.put("path", "/hello");
//        String re=kongAdmin.CreateService(requestBody);
//        JSONObject rejosn=JSON.parseObject(re);
//        System.out.println(rejosn.get("status"));

//
//        列出所有server
//        System.out.println("我的server：" + kongAdmin.ListAllService(1));
//
//        删除某个服务
//
//        System.out.println(kongAdmin.DeleteService("springnxxx"));

//更新服务
//        JSONObject requestBody2 = new JSONObject();
//        requestBody2.put("name", "spring");
//        requestBody2.put("host", "baiduhost");
//        requestBody2.put("port", 8080);
//        requestBody2.put("path", "/");
//        String name = "springnxxx";
//        System.out.println(kongAdmin.PutService(name, requestBody2));
//更新服务的某个部分
//        JSONObject requestBody3 = new JSONObject();
//
//        requestBody3.put("host", "hello");
//
//        String name = "springnxxx";
//        System.out.println(kongAdmin.PatchService(name, requestBody3));

        //创建路由
//        JSONObject requestBody4 = new JSONObject();
//        JSONArray paths = new JSONArray();
//        paths.add("/hello1");
//        JSONArray methods = new JSONArray();
//        methods.add("GET");
//        methods.add("POST");
//        requestBody4.put("name", "routes1");
//        requestBody4.put("paths", paths);
//        requestBody4.put("methods", methods);
//        String serviceName = "kongtest";
//        System.out.println(kongAdmin.CreateRoute(requestBody4, serviceName));

//        列出所有路由在某服务

//        String j=kongAdmin.ListAllRoute( 0);
//        JSONObject requestBody5 = JSON.parseObject(j);
//
//        System.out.println(requestBody5);
////        System.out.println(kongAdmin.ListAllRouteInService("kongtest", 2));
//
//String h="127.989.0908.0:9090";
//
//String host[]=h.split(":");
//
//for(String d:host)    {
//    System.out.println(d);
//}


//        JSONObject requestBody6 = new JSONObject();
//        requestBody6.put("name", "acl");
//
//JSONObject config = new JSONObject();
//JSONArray allow = new JSONArray();
//allow.add("gg");
//config.put("allow", allow);
//requestBody6.put("config", config);
//
//kongAdmin.CreatePluginInService(requestBody6, "kongtest", "acl");

//        JSONObject requestBody7 = new JSONObject();
//        requestBody7.put("name", "key-auth");
//        kongAdmin.CreatePluginInService(requestBody7, "stest1", "s");

//
//        String http="http://123.23.23.2:80";
//        try {
//            new URL(http);
//            try {
//                URL url = new URL(http);
//                System.out.println(url.getHost());
//                System.out.println(url.getPort());
//                System.out.println(url.getPath().equals("") ? "/" :  url.getPath());
//
//            } catch (MalformedURLException e) {
//                throw new RuntimeException(e);
//            }
//            // 是有效的URL
//        } catch (MalformedURLException e) {
//            // 不是有效的URL
//            System.out.println("nnnnnn");
//        }


  // System.out.println(kongAdmin.DeletePluginInConsumer("chenyouwen", "acls", "tttttallow"));
        //服务限流
//        JSONObject requestBody8 = new JSONObject();
//        requestBody8.put("name", "rate-limiting");
//        JSONObject config = new JSONObject();
//        config.put("second", 99);
//        config.put("hour", 99);
//        config.put("policy", "local");
//        requestBody8.put("config", config);
//      String res= kongAdmin.CreatePluginInService(requestBody8,"tttttA","rate-limiting");
//JSONObject rejosn=JSON.parseObject(res);
//System.out.println(rejosn.get("id"));

        //获得信息
       // System.out.println(kongAdmin.getIt("consumers/chenyouwen/acls/tttttallo"));
        String id="{\"key\":\"5a935db4e65378937fedebe71ac13d61\",\"id\":\"27c05ea1-8a22-419d-9f0c-9b608c0293f1\",\"consumer\":{\"id\":\"5ef60b05-5a76-48a5-bce8-3da0ecc8364b\"},\"created_at\":1724149352,\"status\":201}";
        JSONObject requestBody9 =JSONObject.parseObject(id);
    }
}
